import { Injectable } from '@angular/core';
import {Course, CourseInfo, Login, Student} from './student';
import {Observable, of} from 'rxjs';
import {MessageService} from './message.service';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {catchError, map, tap} from 'rxjs/operators';
import {LoginComponent} from './login/login.component';
const httpOptions = {
  headers: new HttpHeaders({'Content-Type': 'application/json'})
};
@Injectable({
  providedIn: 'root'
})
export class HttpService {
  private loginUrl = 'http://123.56.134.7:5000/login';
  // tslint:disable-next-line:variable-name
  private course_selectUrl = 'http://123.56.134.7:5000/course_select_info';
  // tslint:disable-next-line:variable-name
  private course_select_stuidUrl = 'http://123.56.134.7:5000/get_course_select_info_byStuid';
  // tslint:disable-next-line:variable-name
  private course_select_courseidUrl = 'http://123.56.134.7:5000/get_course_select_info_byCourseid';
  // tslint:disable-next-line:variable-name
  private courseInfo_url = 'http://123.56.134.7:5000/courseinfo';
  // tslint:disable-next-line:variable-name
  private get_courseInfo_url = 'http://123.56.134.7:5000/get_courseinfo_byID';
  private studentsUrl = 'http://123.56.134.7:5000/stubasicinfo';
  // tslint:disable-next-line:variable-name
  public get_stubasicinfo_Url = 'http://123.56.134.7:5000/get_stubasicinfo_byID';
  constructor( private http: HttpClient, private messageService: MessageService) { }

  httpPost(formValue: Login): Observable<Response> {
    // 后台接收数据 需要 @RequestBody 标签
    return this.http.post<Response>(this.loginUrl, formValue, httpOptions).pipe();
  }
  private log(message: string) {
    this.messageService.add(`StudentService:${message}`);
  }
  course_select(): Observable<Course[]> {
    return this.http.get<any>(this.course_selectUrl).pipe(
      map(res => res.data),
    );
  }
  course_select_stuid(id: number): Observable<any> {
    const ID = {
      stuid: id
    };
    return this.http.post(this.course_select_stuidUrl, ID, httpOptions).pipe();
  }
  course_select_courseid(id: string): Observable<any> {
    const courseID = {
      courseid: id
    };
    return this.http.post(this.course_select_courseidUrl, courseID, httpOptions);
  }
  course_select1(): Observable<any> {
    return this.http.get<any>(this.course_selectUrl).pipe(
      tap(_ => this.log('获取所有课程信息')),
    );
  }
  getCourses(): Observable<any> {
    return this.http.get<any>(this.courseInfo_url)
      .pipe(
        tap(_ => this.log('获取所有课程信息')),
      );
  }
  getCourseDetail(id: string): Observable<any> {
    const courseID = {
      courseid: id
    };
    return this.http.post(this.get_courseInfo_url, courseID, httpOptions).pipe(
      tap(_ => this.log('通过课程号获取课程信息')
      )) ;
  }
  getStudents(): Observable<any> {
    return this.http.get<any>(this.studentsUrl)
      .pipe(
        tap(_ => this.log('获取所有学生信息')),
      );
  }
  getStudentDetail(id: any): Observable<any> {
    const ID = {
      stuid: id
    };
    return this.http.post(this.get_stubasicinfo_Url, ID, httpOptions).pipe(
      tap(_ => this.log('通过ID获取学生信息')
      )) ;
}}
